﻿using CSRedis;
using SH.Common;
using SH.Common.CommonClass;
using SH.Common.CommonReturn;
using SH.Common.DataDictionary;
using SH.Multitenant.Application.IServices.IStore;
using SH.Multitenant.Infrastructure.IRepository;
using SqlSugar;


namespace SH.Multitenant.Application.Services
{
    public class StoreServices : BaseServices<Store>, IStoreServices
    {
        private readonly IStoreRepository _repository;
        private readonly IDicRepository _dicRepository;
        private readonly CSRedisClient _redis = new CSRedisClient(AppService.Configuration["redisIP"]);
        public StoreServices(IStoreRepository repository, IDicRepository dicRepository) : base(repository)
        {
            _repository = repository;
            _dicRepository = dicRepository; 
        }
        //获取门店列表
        public async Task<List<Store>> GetStoreListAsync()
        {
            return await _repository.GetStoreListAsync();
        }
        //获取租户下的所有的门店列表
        public async Task<List<Store>> GetStoreListByTenantIdAsync()
        {
            string tenantId = _redis.Get<string>("tenantId");
            var list = _redis.HGet<List<Store>>("tenantStoreList", tenantId);
            if (list != null && list.Count() > 0)
            {
                return list;
            }
            list = await _repository.All.ToListAsync();
            _redis.HSet("tenantStoreList", tenantId, list);
            _redis.Expire("tenantStoreList", 60);
            return list;
        }
        //分页获取租户下的门店列表
        public async Task<ApiPaging<List<StoreDTO>>> GetStoreListByTenantIdAsync(string? address, bool? storeStatus, string? storeName, int pageSize, int pageIndex)
        {
            var storeQuery = _repository.All;
            var dicList = _dicRepository.All.Where(x => x.Identification == "HotelType");

            var query = storeQuery.LeftJoin<Dic>(dicList, (a, b) => a.HotelType == b.Id).Where((a, b) => (string.IsNullOrEmpty(address) || a.Address.Equals(address)) && (string.IsNullOrEmpty(storeName) || a.StoreName.Contains(storeName)) && (storeStatus == null || a.StoreStatus == storeStatus)).OrderByDescending(a => a.CreateTime).Select((a, b) => new StoreDTO
            {
                StoreId = a.StoreId,
                StoreName = a.StoreName,
                Address = a.Address,
                HotelType = a.HotelType,
                HotelTypeName = b.Value,
                Introduction = a.Introduction,
                StoreStatus = a.StoreStatus,
                CreateTime = a.CreateTime,
                TenantId = a.TenantId,
            });
            //var result = await _repository.PageQueryAsync(storeQuery, predicate, "createtime desc", pageSIze, pageIndex);
            RefAsync<int> totalCount = 0;
            RefAsync<int> pageCount = 0;
            var pageList = await query.ToPageListAsync(pageIndex, pageSize, totalCount, pageCount);
            ApiPaging<List<StoreDTO>> apiPaging = new ApiPaging<List<StoreDTO>>();
            apiPaging.TotalCount = totalCount;
            apiPaging.PageCount = pageCount;
            apiPaging.Data = pageList;
            return apiPaging;
        }
    }
}
